package Cau9;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Client extends JPanel implements ActionListener, MouseMotionListener, MouseListener {

	DatagramSocket socket;
	DatagramPacket packet;
	int serverPort = 1280;
	Graphics g;
	int kindpen = 0;
	ArrayList<Rectangle> recs = new ArrayList<Rectangle>();
	ArrayList<Oval> ovals = new ArrayList<Oval>();
	ArrayList<Line> lines = new ArrayList<Line>();
	ArrayList<Triangle> triangles = new ArrayList<Triangle>();
	Point spoint = new Point();
	int x1,x2,y1,y2;
	int dx1,dy1,dx2,dy2;
	int a;
	static JPanel botPane = new JPanel();
	static JButton rectangleBt = new JButton("Rectangle");
	static JButton ovalBt = new JButton("Oval");
	static JButton lineBt = new JButton("Line");
	static JButton TriangleBt = new JButton("Triangle");
	static JButton sendBt = new JButton("Send");
	private static final long serialVersionUID = 1L;
	public Client(){
		addMouseListener(this);
		addMouseMotionListener(this);
		rectangleBt.addActionListener(this);
		ovalBt.addActionListener(this);
		lineBt.addActionListener(this);
		TriangleBt.addActionListener(this);
		sendBt.addActionListener(this);
	}
	public void paintComponent(Graphics g){
		for(Rectangle rec : recs){
			rec.drawRectangle(g);
		}
		for(Oval oval : ovals){
			oval.drawOval(g);
		}
		for(Line line : lines){
			line.drawLine(g);
		}
		for(Triangle triangle : triangles){
			triangle.drawTriangle(g);
		}
	}
	public static void main(String[] args) {
		JFrame client = new JFrame("Client-1");
		client.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		client.setBounds(150,150,400,400);
		client.setVisible(true);
		client.setContentPane(new Client());
		botPane.add(rectangleBt);
		botPane.add(ovalBt);
		botPane.add(lineBt);
		botPane.add(TriangleBt);
		botPane.add(sendBt);
		client.add(botPane,BorderLayout.SOUTH);
	}
	@Override
	public void mouseDragged(MouseEvent e) {
		g = getGraphics();
		g.setXORMode(getBackground());
		x2 = e.getX();
		y2 = e.getY();
		dx2 = x2 - spoint.x;
		dy2 = y2 - spoint.y;
		switch(kindpen){
		case 0:	
			Rectangle rec = new Rectangle();
			rec.setSquare(spoint.x, spoint.y, dx1, dy1);
			rec.drawRectangle(g);
			rec.setSquare(spoint.x, spoint.y, dx2, dy2);
			rec.drawRectangle(g);			
			break;
		case 1:
			Oval oval = new Oval();
			oval.setOval(spoint.x, spoint.y, dx1, dy1);
			oval.drawOval(g);
			oval.setOval(spoint.x, spoint.y, dx2, dy2);
			oval.drawOval(g);
			break;
		case 2:
			Line line = new Line();
			line.setLine(spoint.x, spoint.y, x1, y1);
			line.drawLine(g);
			line.setLine(spoint.x, spoint.y, x2, y2);
			line.drawLine(g);
			break;
		case 3:
			Triangle triangle = new Triangle();
			triangle.setTriangle(spoint.x, spoint.y, dx1, dy1);
			triangle.drawTriangle(g);
			triangle.setTriangle(spoint.x, spoint.y, dx2, dy2);
			triangle.drawTriangle(g);
		default: break;
		}
		x1 = x2; y1 = y2;
		dx1 = dx2; dy1 = dy2;
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		spoint = e.getPoint();
		x1 = spoint.x;
		y1 = spoint.y;
		dx1= 0;dy1=0;
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		switch(kindpen){
		case 0:	
			Rectangle rec = new Rectangle();
			rec.setSquare(spoint.x, spoint.y, dx1, dy1);
			recs.add(rec);						
			break;
		case 1:
			Oval oval = new Oval();
			oval.setOval(spoint.x, spoint.y, dx1, dy1);
			ovals.add(oval);
			break;
		case 2:
			Line line = new Line();
			line.setLine(spoint.x, spoint.y, x1, y1);
			lines.add(line);
			break;
		case 3:
			Triangle triangle = new Triangle();
			triangle.setTriangle(spoint.x, spoint.y, dx1, dy1);
			triangles.add(triangle);
		default: break;
		}
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(e.getActionCommand().equals("Rectangle")) kindpen = 0;
		else if(e.getActionCommand().equals("Oval")) kindpen = 1;
		else if(e.getActionCommand().equals("Line")) kindpen = 2;
		else if(e.getActionCommand().equals("Triangle")) kindpen = 3;
		else
			try {
				InitPackage();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	}
	private void InitPackage() throws IOException {
		try {
			socket = new DatagramSocket();
			DataPacket data = new DataPacket();
			data.setType(1);
			data.setRectangles(recs);
			data.setOvals(ovals);
			data.setLines(lines);
			data.setTriangles(triangles);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(data);
			DataPacket size = new DataPacket();
			size.setType(0);
			size.setSize(baos.size());
			ByteArrayOutputStream bsize = new ByteArrayOutputStream();
			ObjectOutputStream osize = new ObjectOutputStream(bsize);
			osize.writeObject(size);
			packet = new DatagramPacket(bsize.toByteArray(), bsize.size(), InetAddress.getLocalHost(), serverPort);
			socket.send(packet);
			packet = new DatagramPacket(baos.toByteArray(), baos.size(), InetAddress.getLocalHost(), serverPort);
			socket.send(packet);
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
